package com.xyht.sca_s.student_manage_system.modules.asset.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class AssetUtil {

    public String createAssetStorageID(String category_id,
                                       Date now_date,
                                       Integer in_storage_method,
                                       Integer in_storage_count) throws NoSuchAlgorithmException {
        return createAssetStorageID(category_id, now_date, in_storage_method, in_storage_count,0,0);
    }
    /**
     * @Param in_storage_count 入库批次 单一入库为0
     *
     * */
    public String createAssetStorageID(String category_id,
                                       Date now_date,
                                       Integer in_storage_method,
                                       Integer in_storage_count,
                                       Integer goods_count,
                                       Integer goods_detail_count) throws NoSuchAlgorithmException {
        StringBuilder stringBuilder = new StringBuilder();
        if (category_id.length() < 3) {
            stringBuilder.append(category_id);
            stringBuilder.append("0000");
        } else {
            stringBuilder.append(category_id);
        }

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String date_str = simpleDateFormat.format(now_date).replace("-", "");
        stringBuilder.append(date_str);

        //入库方式  1：固定资产导入入库  2：固定资产申请入库 3：固定资产单个入库
        //         4：易耗品导入入库  5：易耗品申请入库  6：易耗品单个入库
        stringBuilder.append(in_storage_method);

        //入库批次 单个入库批次为00
        String in_storage_count_str;
        if (in_storage_count.equals(0)){
            in_storage_count_str = "00";
        }else{
            if (in_storage_count < 10){
                in_storage_count_str = "0"+in_storage_count;
            }else{
                in_storage_count_str = in_storage_count.toString();
            }
        }
        stringBuilder.append(in_storage_count_str);

        //物品导入顺序
        String goods_count_str;
        if (goods_count.equals(0)){
            goods_count_str = "00";
        }else{
            if (goods_count < 10){
                goods_count_str = "0"+goods_count;
            }else{
                goods_count_str = goods_count.toString();
            }
        }
        stringBuilder.append(goods_count_str);


        //物品详情数量顺序
        String goods_detail_count_str;
        if (goods_detail_count.equals(0)){
            goods_detail_count_str = "00";
        }else{
            if (goods_detail_count < 10){
                goods_detail_count_str = "0"+goods_detail_count;
            }else{
                goods_detail_count_str = goods_detail_count.toString();
            }
        }
        stringBuilder.append(goods_detail_count_str);

        // 创建 SecureRandom 对象，并设置加密算法
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
        int number = random.nextInt(899999) + 100000;
        int random_num = random.nextInt(9);
        stringBuilder.append(number);
        stringBuilder.append(random_num);
        return stringBuilder.toString();
    }


}
